// let ext = require("./../ext.json");
var appid = '2019050964453150';
var base = require('/config.js');
exports.transTime = function (val) {
  var date = '';
  if (val) {
    date = new Date(val);
  } else {
    date = new Date();
  };
  var Y = date.getFullYear();
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() ;
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s; 
}

exports.timeDown = function (val) {
  var nowTime = new Date();
  var createdTime = new Date(val);
  if (nowTime - createdTime < 0 || nowTime - createdTime > 1800000) {
    return 0;
  };
  var differ = nowTime - createdTime;
  differ = 1000 * 60 * 30 - differ;
  var minute = Math.floor(differ / 60000);
  minute = minute < 10 ? '0' + minute : minute;
  var second = Math.floor((differ - minute * 60000) / 1000);
  second = second < 10 ? '0' + second : second;
  var resulte = minute + ':' + second;
  if (minute == NaN || second == NaN) {
    resulte = 0;
  }
  return minute + ':' + second;
}

// 获取今天的时间
exports.toDayTime = function () {
  var times = new Date();
    var year = times.getFullYear();
    var month = times.getMonth() < 10 ? '0' + (times.getMonth() + 1) : (times.getMonth() + 1);
    var day = times.getDate() < 10 ?  '0' + times.getDate() : times.getDate();
    return year + '-' + month + '-' + day;
}

// 获取目标月的天数
exports.getCountDays = function(time) {
  var curDate = new Date(time);
  /* 获取当前月份 */
  var curMonth = curDate.getMonth();
  /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
  curDate.setMonth(curMonth + 1);
  /* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
  curDate.setDate(0);
  /* 返回当月的天数 */
  return curDate.getDate();
}

// 获取url后的参数
exports.getParams = function (url){ 
  var num = url.indexOf("?");
  url = url.substr(num); //获取url中"?"符后的字串
  var theRequest = new Object();
  var strs = '';
  if (num > 0) {
    strs = url.substr(1).split("&");
  } else {
    strs = url.split("&");
  }
  for(let i = 0; i < strs.length; i ++) {
    theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
  }
  return theRequest;
}

exports.getCodeParams = function (url){
  var theRequest = new Object();
  var strs = '';
  strs = url.split("&");
  for (let i = 0 ; i < strs.length ; i++) {
    let params = strs[i].split("=");
    theRequest[params[0]] = params[1];
  }
  return theRequest;
}

// 返回固定格式的时间02-01(周一)
exports.setTimeList = function (val){ 
  var date = '';
  if (val) {
    date = new Date(val);
  } else {
    date = new Date();
  };
  var day = new Date(date).getDay(), text = "";
  switch (day) {
    case 0:
      text = "周日";
      break;
    case 1:
      text = "周一";
      break;
    case 2:
      text = "周二";
      break;
    case 3:
      text = "周三";
      break;
    case 4:
      text = "周四";
      break;
  case 5:
      text = "周五";
      break;
    case 6:
      text = "周六";
      break;
  }
  var Y = date.getFullYear();
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() ;
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return  {
    day: M + '-' + D + '(' + text + ')',
    value: Y + '-' + M + '-' + D
  }; 
}

exports.setColor = function () {
  // my.setNavigationBar({
  //   backgroundColor: ext.ext.themeColor
  // })
}
exports.appRequest  = function (methods, url, data,  successCallback, failCallback) {
  my.request({
    url: url,
    method: methods,
    header: {        
    'content-type': methods == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded'
    },
    dataType: 'json',
    data: data,
    success: function (res) {
      my.hideLoading();
      if (res.data.errno == 1) {
        successCallback(res); 
      } else {
        my.alert({
          title: `${res.data.data.res}`
        });
        console.log(`${res.data.data.res}`);
      };
    },
    fail: function (err) {

      my.hideLoading();
      // my.alert({
      //   title: `网络错误，请稍后再试[错误代码：${url}]`
      // });
      my.alert({
        title: `网络错误，请稍后再试`
      });

    }

  })

},
exports.getDataNew = function (url, data, successCallback, failCallback) {
  let params = {
    c: 'entry',
    a: 'wxapp',
    m: 'zh_jdgjb',
    i:'2',
    t:'0',
    v:'7.4.0',
    from:'wxapp',
    appid: appid,
    do: url,
  };
  data = Object.assign(data, params);
  my.request({
    url: base.siteroot,
    method: 'GET',
    data: data,
    headers: {
      appid: appid
    },
    dataType: 'json',
    success: function(res) {
      my.hideLoading();
      if (res.data.errno == 1) {
        successCallback(res); 
      } else {
        my.alert({
          title: `${res.data.data.res}`
        });
        console.log(`${res.data.data.res}`);
      };
    },
    fail: function(res) {
      my.hideLoading();
      console.log(JSON.stringify(res));
      my.alert({
        title: `网络错误，请稍后再试[错误代码：${url}]`
      });
      // my.alert({
      //   title: `网络错误，请稍后再试`
      // });
    }
  });
}
